﻿using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using IntelligentEscalator.BUS;
using IntelligentEscalator.Models;

namespace IntelligentEscalator.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class AreaController : ControllerBase
    {
        public IHttpContextAccessor _contextAccessor;
        private JwtSettings _jwtSettings;
        public AreaController(IHttpContextAccessor contextAccessor, IOptions<JwtSettings> jwtSettingsAccesser)
        {
            _contextAccessor = contextAccessor;
            _jwtSettings = jwtSettingsAccesser.Value;
        }

        /// <summary>
        /// 新增区域信息
        /// </summary>
        /// <param name="areainfo">区域信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddArea")]
        public ResponseMessage AddArea(AreaInfo areainfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                string username = HttpContext.Session.GetString("UserName").ToString();
                areainfo.creationPerson = username;
                ElevatorAreaBUS BUS = new ElevatorAreaBUS();
                message = BUS.AddArea(areainfo);
                if (message.errcode == 0)
                {
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "新增区域:"+ areainfo.areaName;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "新增区域失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增区域";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 编辑区域信息
        /// </summary>
        /// <param name="areainfo">区域信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateArea")]
        public ResponseMessage UpdateArea(AreaInfo areainfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaBUS BUS = new ElevatorAreaBUS();
            try
            {
                message = BUS.UpdateArea(areainfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "修改区域信息:" + areainfo.areaName;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "修改区域信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改区域信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 删除区域
        /// </summary>
        /// <param name="areainfo">区域信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DelArea")]
        public ResponseMessage DelArea(AreaInfo areainfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaBUS BUS = new ElevatorAreaBUS();
            try
            {
                message = BUS.DelArea(areainfo.id);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "删除区域信息:" + areainfo.areaName;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "删除区域信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除区域信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);

            }
            return message;
        }

        /// <summary>
        /// 查询区域
        /// </summary>
        /// <param name="areainfo">区域信息</param>
        /// <returns>ResponseMessage对象</returns>
        [HttpPost]
        [Route("QueryArea")]
        public ResponseMessage QueryArea(AreaInfo areainfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                ElevatorAreaBUS BUS = new ElevatorAreaBUS();
                message = BUS.QueryArea();
                string data = JsonConvert.SerializeObject(message.data);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询区域信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询区域信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 批量删除区域
        /// </summary>
        /// <param name="ids">区域id</param>
        /// <returns>ResponseMessage对象</returns>
        [HttpPost]
        [Route("BatchDelArea")]
        public ResponseMessage BatchDelArea(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaBUS BUS = new ElevatorAreaBUS();
            try
            {
                message = BUS.BatchDelArea(ids);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "批量删除区域信息";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "批量删除区域失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除区域信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }
    }
}
